<?php

namespace app\common\model\events;

use app\common\model\LevelConfig;
use app\common\model\user\Level;
use fast\Date;
use think\Model;


class Events extends Model
{





    // 表名
    protected $name = 'events';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'starttime_text',
        'endtime_text',
        'vip_fee',
        'state_text'
    ];

    protected static function init()
    {
        self::beforeInsert(function ($row) {
            if($row['endtime']>$row['starttime']){
                exception('报名结束时间不能大于活动开始时间');
            }
            if (isset($row['endtime'])) {
                if ($row['endtime'] <= time()) {
                    $row['state'] = '2';
                } else {
                    $row['state'] = '1';
                }
            }
        });
        self::beforeUpdate(function ($row) {
            if($row['endtime']>$row['starttime']){
                exception('报名结束时间不能大于活动开始时间');
            }
            if (isset($row['endtime'])) {
                if ($row['endtime'] <= time()) {
                    $row['state'] = '2';
                } else {
                    $row['state'] = '1';
                }
            }
        });
    }
    public function getVipFeeAttr($value, $data)
    {
        $level = LevelConfig::get(1);
        if ($level->discount > 0 && $data['price'] > 0) {
            $discount = bcdiv($level->discount, 100, 2);
            $vip_fee = bcmul($data['price'], $discount, 2);
            if ($vip_fee > 0) {
                return $vip_fee;
            }
        }
        return $data['price'];
    }
    public function getImageAttr($value, $data)
    {
        if ($value) {
            return cdnurl($value, true);
        }
        return '';
    }
    public function getStateList()
    {
        return ['1' => __('State 1'), '2' => __('State 2')];
    }

    public function sign()
    {
        return $this->hasMany('app\common\model\events\Sign', 'e_id', 'id');
    }

    public function getStarttimeTextAttr($value, $data)
    {
        $week = [0 => '星期日', 1 => '星期一', 2 => '星期二', 3 => '星期三', 4 => '星期四', 5 => '星期五', 6 => '星期六',];
        return Date('m.d', $data['starttime']) . ' ' . $week[Date('w', $data['starttime'])] . ' ' . Date('H:i', $data['starttime']);
        // $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
        // return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setStarttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
}
